﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Persons.DS;
using Persons.Classes;
using Persons.Interfaces;
using Persons.DataStore;
using System.IO;

namespace PersonsWinForm
{
    public partial class FViewListOfPersons : Form
    {
        static IDS ds = new DS_MOCK();
        //static IDS ds = new DS_NET_CLIENT();
        public static ListOfPersons list;
        public static Point location = new Point(0, 0);
        public static int PanelWidth=0;
        public static int PanelHeight=0;

        public FViewListOfPersons()
        {
            list = new ListOfPersons();
            ListOfPersons.ds = ds;
            //Server.Server.ds = ListOfPersons.ds;
            InitializeComponent();
        }

        private void FViewListOfPersons_Load(object sender, EventArgs e)
        {
            SSlbl_Status.Text = "Текущий источник данных " + ListOfPersons.ds.GetType().Name.ToString();
            List<Persona> l = list.Read();
            Pnl_Data.Controls.Clear();
            location = new Point(0, 0);
            PanelWidth = Pnl_Data.Width / 5 - 5;
            PanelHeight = Pnl_Data.Height / 4 - 4;
            DrawPersons dp = new DrawPersons(null);
            Pnl_Data.Controls.Add(dp);
            dp.Draw();

            for (int i = 0; i < list.Count; i++)
            {
                dp = new DrawPersons(list[i]);
                Pnl_Data.Controls.Add(dp);
                dp.Draw();
            }
            //DataSourceVisible(true, true, false);
        }

        private void DataSourceVisible(bool DB_Visible, bool CSV_Visible, bool MOCK_Visible)
        {
            dataBaseToolStripMenuItem.Visible = DB_Visible;
            cSVFileToolStripMenuItem.Visible = CSV_Visible;
            MOCKToolStripMenuItem.Visible = MOCK_Visible;
        }

        private void ChangeDataSource()
        {
            bool DB_Visible = false, CSV_Visible = false, MOCK_Visible = false;
            switch (ds.GetType().Name)
            {
                case "DS_DB":
                    {
                        DB_Visible = true;
                        break;
                    }
                case "DS_CSV":
                    {
                        CSV_Visible = true;
                        break;
                    }
                default:
                    {
                        MOCK_Visible = true;
                        break;
                    }
            }
            DataSourceVisible(DB_Visible, CSV_Visible, MOCK_Visible);
        }

        private void cSVFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog OD = new OpenFileDialog();
            OD.Filter = "CSV Files|*.csv";
            if (OD.ShowDialog() == DialogResult.OK)
            {
                if ((OD.FileName != "") && (File.Exists(OD.FileName)))
                {
                    ListOfPersons.ds = Factory.GetInstance("csv", OD.FileName);
                    //Server.Server.ds = ListOfPersons.ds;
                    FViewListOfPersons_Load(sender, e);
                }
            }
        }

        private void NETToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FSetConnection fsc = new FSetConnection();
            fsc.ShowDialog(this);
            if (fsc.DialogResult == DialogResult.OK)
            {
                FViewListOfPersons_Load(sender, e);
            }
        }

        private void MOCKToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListOfPersons.ds = new DS_MOCK();
            //Server.Server.ds = ListOfPersons.ds;
            FViewListOfPersons_Load(sender, e);
        }

        private void dataBaseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListOfPersons.ds = new DS_DB(@"Provider=SQLNCLI10.1;Data Source=JULIA-PC\MSSQLHEDGEHOG;Integrated Security=SSPI;Initial Catalog=ListOfPersona");
            FViewListOfPersons_Load(sender, e);
        }
    }
}
